<?php
function set_history($data){
    if(!$data || !is_array($data)){
        return false;
    }
    //判断cookie类里面是否有浏览记录
    if(cookie('history')){
        $history = unserialize(cookie('history'));
        array_unshift($history, $data); //在浏览记录顶部加入
        /* 去除重复记录 */
        $rows = array();
        foreach ($history as $v){
            if(in_array($v, $rows)){
                continue;
            }
            $rows[] = $v;
        }
        /* 如果记录数量多余5则去除 */
        while (count($rows) > 5){
            array_pop($rows); //弹出
        }
        cookie('history',serialize($rows));
    }else{
        $history = serialize(array($data));
        cookie('history',$history);
    }
}
function get_history(){
    return unserialize(cookie('history'));
}
function order_sn(){
    return date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
}
function _uploadOne($name,$id,$diff=true) {
    if($id && $diff){
        if(strtolower(CONTROLLER_NAME)=='user'){
            $img = M('users')->where(array('id'=>$id))->getField($name);
            if($img){
                unlink('./Public/upload/'.strtolower(CONTROLLER_NAME).'/'.$img);
            }
        }
    }
    $upload = new \Think\Upload();// 实例化上传类
    $upload->maxSize = 3145728;
    $upload->rootPath = './Public/upload/';
    $upload->savePath = strtolower(CONTROLLER_NAME).'/';
    $upload->saveName = array('uniqid','');
    $upload->exts     = array('jpg','gif','png','jpeg');
    $upload->autoSub  = false;//子目录
    $upload->subName  = array('Public','Ymd');
    $info   =   $upload->uploadOne($_FILES[$name]);
    if(!$info) {// 上传错误提示错误信息
        $this->error($upload->getError());
    }else{// 上传成功 获取上传文件信息
        $image = new \Think\Image();
        $image->open('./Public/upload/'.strtolower(CONTROLLER_NAME).'/'.$info['savename']);
        $image->thumb(640,640)->save('./Public/upload/'.strtolower(CONTROLLER_NAME).'/s_'.$info['savename']);
        return 's_'.$info['savename'];
    }
}
function weixin(){
    if (strstr($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') && $wechat_list[0]['wait_access']) {
        $wechat_config = $wechat_list[0];
        $this->weixin_config = $wechat_config;
        $this->assign('wechat_config', $wechat_config); // 微信配置
        if ($wechat_config && !$_SESSION['openid']) {
            //去授权获取openid
            $wxuser = $this->GetOpenid();
            //获取用户昵称
            session('subscribe', $wxuser['subscribe']); // 当前这个用户是否关注了微信公众号
            //微信自动登录
            $data = array(
                'openid' => $wxuser['openid'], //支付宝用户号
                'oauth' => 'weixin',
                'nickname' => trim($wxuser['nickname']) ? trim($wxuser['nickname']) : '微信用户',
                'sex' => $wxuser['sex'],
                'head_pic' => $wxuser['headimgurl'],
            );
            $userModel = new UserModel();
            $data = $userModel->thirdLogin($data);
            if ($data['status'] == 1) {
                session('user', $data['result']);
                setcookie('user_id', $data['result']['user_id'], null, '/');
                setcookie('is_distribut', $data['result']['is_distribut'], null, '/');
                setcookie('uname', $data['result']['nickname'], null, '/');
                // 登录后将购物车的商品的 user_id 改为当前登录的id
                M('cart')->where("session_id = '{$this->session_id}'")->save(array('user_id' => $data['result']['user_id']));
            }
        }
        // 微信Jssdk 操作类 用分享朋友圈 JS
        $jssdk = new \Mobile\Model\WxModel($this->weixin_config['appid'], $this->weixin_config['appsecret']);
        $signPackage = $jssdk->GetSignPackage();
        $this->assign('signPackage', $signPackage);
    }
}